package com.wbc;

import com.wbc.config.DynamicCompiler;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class DynamicCompilerTest {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, IOException, InterruptedException {


        DynamicCompiler compiler = new DynamicCompiler();

        String java = "package com.wbc;\n" +
                "\n" +
                "public class DemoClass {\n" +
                "    @Override\n" +
                "    public String toString() {\n" +
                "        return \"这是一个实例化的类\";\n" +
                "    }\n" +
                "\n" +
                "    public void say(){\n" +
                "        System.out.println(\"say\");\n" +
                "    }\n" +
                "\n" +
                "    public void ding(){\n" +
                "        System.out.println(\"ding\");\n" +
                "    }\n" +
                "}\n";

        String fullName1 = "com.wbc.DemoClass";

        try{
            Class<?> aClass = compiler.forName("com.wbc.DemoClass");
            System.out.println("加载成功");
        }catch(Exception e){
            System.out.println("加载失败");
        }


        Class clz1 = compiler.compileAndLoad(fullName1, java);

        try{
            Class<?> aClass = compiler.forName("com.wbc.DemoClass");
            System.out.println("加载成功");
        }catch(Exception e){
            e.printStackTrace();
            System.out.println("加载失败");
        }
        Object o = clz1.getConstructor().newInstance();
        System.out.println(o);
        Method say = clz1.getMethod("say");
        Object invoke = say.invoke(o);

    }
}

